#ifndef USB_CMD_H
#define USB_CMD_H

#include "main.h"
#include "cmsis_os.h"

#include "configs.h"
#include "string.h"
#include "bsp_usb.h"
#include "bsp_usart.h"
#include "wait_time_func.h"




#define RECV_HEADER 0xFA
#define SEND_HEADER 0xAF

#define RECV_TAIL 0xAF
#define SEND_TAIL 0xFA

#define RECV_ID_CHECK 0x00
#define SEND_ID_CHECK 0x00

#define RECV_ID_INDEX 0x01
#define SEND_ID_INDEX 0x01

#define RECV_ID_GET_LOG 0x02
#define SEND_ID_SEND_LOG 0x02

#define RECV_ID_SET_PARAM 0x03

#define RECV_ID_OPEN_NEW_N_LOG 0x04
#define RECV_ID_SET_N_LOG 0x05
#define RECV_ID_WRITE_DATA_TO_NORMAL 0x06
#define RECV_ID_CLEAR_CURRENT_N_LOG 0x07
#define RECV_ID_RESET_FS 0x08

#define RECV_ID_ONLINE_MONITOR 0x09
#define SEND_ID_ONLINE_MONITOR 0x09

#define RECV_ID_PARAM_INIT 0xFE
#define RECV_ID_RESET_DART 0xFF

typedef enum{
    USART,
    USB
}Data_from;

typedef enum{
    WAIT_MESSAGE,
    HANDLE_MESSAGE_QUEUE
}Manger_State;

typedef struct Cmd_Message_pack{
    Data_from data_from;
    uint8_t message_in[600];
    uint8_t data_length;
    bool avilable;
}Cmd_Message_pack;

typedef struct{
    Cmd_Message_pack message_pack_queue[10];
    uint8_t message_num;
    Data_from data_from;
    Manger_State manager_state;
    Cmd_Message_pack to_send_pack;
    bool allow_send;
}Message_manager;

void message_queue_put(Cmd_Message_pack pack);
Cmd_Message_pack message_queue_pop(void);
void message_queue_handle(void);

void message_handle(Cmd_Message_pack* pack);
void send_file_index(Data_from data_from);
void update_param_data(uint8_t* raw_data, uint8_t data_len);
void send_target_log_data(uint16_t tar_log_id, Data_from data_from);
void online_send_monitor_data_flag_change(uint8_t flag, Data_from data_from);
void update_param_data_by_index(uint8_t* raw_data, uint16_t index);
void write_data_to_current_normal(uint8_t* raw_data, uint8_t data_len);
void set_normal_log(uint16_t log_id);
void clear_current_nlog(void);
void open_new_normal_log(void);
void param_init(void);
void let_file_system_init(void);

extern Message_manager message_manager;

#endif
